package com.shop.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.shop.admin.service.AdminService;
import com.shop.admin.vo.AdminVO;
import com.shop.framework.util.Logger;

/**
 * @author geryon
 * 관리자계정 컨트롤러 사이트의 관리자 계정을 관리한다
 */
@Controller
@RequestMapping("/ADMIN")
public class AdminController {

	private final static int LIST_ROW_CNT = 20; // 게시물 리스트 갯수
	
	@Autowired
	private AdminService service;

	/**
	 * 관리자 페이지 호출
	 * 리스트 화면을 호출한다
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/")
	public String requestScreen(Model model) {
		return "admin/admin_member_list";
	}

	/**
	 * 관리자 리스트
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/list")
	public Model listAdmin(Model model) {
		
		List<AdminVO.AdminMemberItem> list = service.listMember();
		model.addAttribute("MODEL", list);
		return model;
	}
	
	/**
	 * 관리자 등록
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/add")
	public Model addAdmin(Model model,
							@RequestBody AdminVO.AdminMemberItem adminMemberItem) {
		
		// 사용자 체크
		int isUserCnt = service.isUserCnt(adminMemberItem.getId(), adminMemberItem.getPw());
		if (isUserCnt == 0) {
			service.addMember(adminMemberItem);
			model.addAttribute("PROCESS_CODE",true);
		} else {
			model.addAttribute("PROCESS_CODE",false);
		}
		
		return model;
	}

	/**
	 * 관리자 정보 상세조회
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/{admin_seq}")
	public Model getAdmin(Model model,
							@PathVariable ("admin_seq") String admin_seq) {
		Logger.getLogger().debug("SEQ === " + admin_seq);
		model.addAttribute("MODEL",(AdminVO.AdminMemberItem)service.getMember(admin_seq));
		return model;
	}

	/**
	 * 관리자 수정
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value="/{admin_seq}")
	public Model updateAdmin(Model model,
							@PathVariable ("admin_seq") String admin_seq,
							@RequestBody AdminVO.AdminMemberItem memberItem) {
		if (admin_seq.equals(""+memberItem.getSeq())) {
			service.updateMember(memberItem);
			model.addAttribute("PROCESS_CODE", true);
		} else {
			model.addAttribute("PROCESS_CODE", false);
		} 
		return model;
	}

	/**
	 * 관리자 삭
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value="/{admin_seq}")
	public Model deleteAdmin(Model model,
							@PathVariable ("admin_seq") String admin_seq) {
		service.deleteMember(admin_seq);
		model.addAttribute("PROCESS_CODE", true);
		return model;
	}
	
	
}
